<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Scan; 

class Friendexchange extends Base
{
    public function Friendexchange()
    {
global $_G, $_P;
$system    = $_G['system'];
$fans = $this->checkuser();
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid = intval($_P['themeid']);
// $theme = Theme::getThemeById($themeid);
$config = Theme::getConfig($themeid);
$giftid = $_P['giftid'];
$result = array();

$pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
$fans = db_find("SELECT * FROM ".tablename(t_fans)."
		WHERE themeid = :themeid and openid = :openid",$pm);
		
if($system['friend'] !=1){
	$result = array(
		"errcode"=>2,
		"msg"=>'功能未开启',
	);
	return_msg($result, '', 'ajax');
}

if(empty($giftid)){
	$result = array(
		"errcode"=>2,
		"msg"=>'参数错误',
	);
	return_msg($result, '', 'ajax');
}

if(empty($fans['username'])){
	$result = array(
		"errcode"=>2,
		"msg"=>'还未报名',
	);
	return_msg($result, '', 'ajax');
}


$sql = "SELECT * FROM " . tablename ( t_gift ) . " 
							WHERE id = '{$giftid}' ORDER BY displayorder ";
$gift = db_find ( $sql );

$friendshengyu = $fans['friendnum']-$fans['friendshiyong'];

if($gift['duihuan']>$friendshengyu ){
	$result = array(
		"errcode"=>2,
		"msg"=>'数量不够',
	);
	return_msg($result, '', 'ajax');
}

if($gift['stock']<1 ){
	$result = array(
		"errcode"=>2,
		"msg"=>'库存不足',
	);
	return_msg($result, '', 'ajax');
}
if($gift['duihuan']<1 ){
	$result = array(
		"errcode"=>2,
		"msg"=>'礼品设置参数错误',
	);
	return_msg($result, '', 'ajax');
}


$sql = "SELECT * FROM " . tablename (t_record) . "
							WHERE themeid =:themeid
									and uid = :uid and (status > 0 or status=-2) order by id desc limit 1";
$isorder = db_find($sql, array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']));

if (empty($isorder) && $config['friendorder']==0){
	$result = array(
		"errcode"=>2,
		"msg"=>'您还没有支付报名',
	);
	return_msg($result, '', 'ajax');
}

    $params = array();
    
    $params[':uid'] = $_G['member']['uid'];
    $params[':giftid'] = $giftid;
    $params[':themeid'] = $themeid;
    $friend = db_find("SELECT * FROM ".tablename(t_exchange)."
		WHERE uid = :uid and themeid = :themeid and giftid = :giftid",$params);

	if(empty($friend)){
        $data = array(
            'themeid'   => $themeid,
            'uniacid'   => $_G['mpid'],
            'uid'       => $_G['member']['uid'],
            'openid'    => $_G['openid'],
            'nickname'  => $fans['nickname'],
            'avatar'    => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
            'status'    => 1,
            'addtime'   => TIMESTAMP,
            'giftid'  => $giftid,
            'giftname'  => $gift['giftname'],
            'duihuan'  => $gift['duihuan'],
        );
        // if($config['iconlimit'] > 0){
        //     $todaynum = Scan::getFpenidTodayNum($themeid,$fopenid);
        //     if($todaynum < $config['iconlimit']){
        //         $data['status'] = 1;
        //     }
        // }else{
            // $data['status'] = 1;
        // }
        $insertid = db_insert(t_exchange, $data);

		$pm = array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']);
		
		db_query("UPDATE ".tablename(t_fans)." SET friendshiyong = friendshiyong + " . $gift['duihuan'] . " WHERE themeid =:themeid and uid = :uid",$pm);

		db_query("UPDATE ".tablename(t_gift)." SET stock = stock - 1 WHERE id = " . $giftid );

		$result = array(
			"errcode"=>1,
			"msg"=>'成功兑换',
		);
		return_msg($result, '', 'ajax');
    }else{
		$result = array(
			"errcode"=>2,
			"msg"=>'不可重复兑换',
		);
		return_msg($result, '', 'ajax');
    }
}
}